<template>
  <div>
    <hy-order-descriptions :orderId='orderId' v-if='loaded'></hy-order-descriptions>

    <a-divider orientation='left'>发货信息</a-divider>
    <f-table ref='orderDrugs' rowKey='orderDetailsId' :scroll='{x:true}' :showTotal='true' setting-key='hy_order_details' handler='hyOrderReturnHandler' :tableColumns='orderDetailsColumns' :operator='operator' :params='queryParam' :api='api.hyOrderDetails' :pageSize='80' :autoLoad='false' :showPagination='false' :multiple='false'>
       <span slot='drugType' slot-scope='text, record, index'>
           <a-tag color='#2db7f5' v-if='text=="ZY"'>自营</a-tag>
           <a-tag color='#FF0000' v-if='text=="ZS"'>招商</a-tag>
       </span>
    </f-table>

    <a-modal title='退货回款申请' v-model='tuihuoHuikuanModal' v-if='tuihuoHuikuanModal' width='800px' height='400px' @cancel='this.tuihuoHuikuanModal = false'>
      <f-form ref='returnForm2' :action='api.payment' v-if='tuihuoHuikuanModal' handlerName='hyTuiHuoPaymentSaveHandler' :form-key='this.formItem.paymentId'>
        <f-form-item label='回款数量' type='number' disabled data-type='number' name='returnNum' :value='formItem.saleNum' />
        <f-form-item label='回款金额' type='number' disabled data-type='number' name='returnAmount' :value='formItem.noReturnAmount' />
        <f-form-item label='回款日期' type='date' name='amountDate' :required='true' :value='formItem.returnDate' />
      </f-form>
    </a-modal>

    <a-modal title='商品退货申请' v-model='tuihuoModal' v-if='tuihuoModal' width='650px' height='400px' @cancel='tuiHandleCancel' @ok='tuiHandleSubmit'>
      <f-form ref='tuibuForm' :action='api.hyOrderDetails.tuibuhuo' v-if='tuihuoModal' :form-key='this.tuiFormItem.orderDetailsId'>
        <f-form-item label='退货数量' type='number' data-type='number' name='saleNum' :required='true' :disabled='true' :value='tuiFormItem.saleNum' :max='tuiFormItem.saleNum' />
        <f-form-item label='退货日期' type='date' name='saleDate' :value='tuiFormItem.saleDate' />
      </f-form>
    </a-modal>

    <a-modal title='订单票折' v-model='discountModal' v-if='discountModal' width='800px' height='400px' @cancel='this.discountModal = false' @ok='handleDiscountSubmit'>
      <f-form ref='discountForm' :action='api.hyOrderDetails.discount' v-if='discountModal' handlerName='hyTuiHuoPaymentSaveHandler' :form-key='this.formItem.orderDetailsId'>
        <f-form-item label='票折金额' type='number' data-type='number' name='saleAmount' :value='formItem.saleAmount' />
      </f-form>
    </a-modal>

    <process-image ref='processImg' v-if='processImageVisible' :visible.sync='processImageVisible' :processInstanceId='processInstanceId'></process-image>


  </div>

</template>

<script>
import api from "../../js/api"
import ProcessImage from "../../../flowable/process/process_image"
import HyOrderDescriptions from "@/views/module/haiying/order/details/hyorder_descriptions"

export default {
  name: "HyOrderReturn",
  components: { HyOrderDescriptions, ProcessImage },
  data() {
    let me = this
    //操作按钮
    let operator = []
    return {
      api: Object.assign(api, this.api),
      //列表初始化查询参数
      queryParam: {},
      orderId: null,
      loaded: false,
      processImageVisible: false,
      processInstanceId: null,
      saleType: "",
      orderDetailsColumns: [],
      fileIds: [],//上传文件ID
      tuihuoHuikuanModal: false,
      chaiModal: false,
      formItem: this.getFormItem(),
      tuiFormItem: this.getTuiFormItem(),
      orderForm: {},
      tuihuoModal: false,
      discountModal: false,
      operator: operator,
      columns2: []
    }
  },
  created() {
  },
  computed: {
    title: function() {
      return this.saleType == "CHC" ? "退货" : "补货"
    }
  },
  methods: {
    getTuiFormItem() {
      return {
        orderDetailsId: "",
        saleDate: "",
        saleNum: ""
      }
    },
    getFormItem() {
      return {
        returnId: "",
        saleNum: 0,
        noReturnNum: 0,
        returnType: 0,
        noReturnAmount: "",
        sendDate: "",
        saleAmount: 0,
        hkImage: []
      }
    },
    getColumns2() {
      let columns = []
      let column1 = [
        {
          title: "操作",
          align: "center",
          dataIndex: "action",
          scopedSlots: { customRender: "action" }
        }]

      return Object.assign(columns, column1)
    },
    setData(data) {
      if (data && data.orderId) {
        this.queryParam.orderId = data.orderId
        this.orderId = data.orderId
        this.loaded = true
        this.columns2 = this.getColumns2()
        this.orderDetailsColumns = this.getCol(data)
        this.$refs["orderDrugs"].refresh(false)
      }
    },
    getCol(data) {
      let saleManId = !data.saleManId
      let sendType = data.sendType
      let deptType = this.orderForm.deptType
      let isZbj = false
      if (sendType == 3) {
        isZbj = true
      }
      return [
        {
          title: "商品名称",
          align: "left",
          ellipsis: true,
          width: "140px",
          dataIndex: "drugsName"
        },
        {
          title: "规格",
          align: "left",
          ellipsis: true,
          width: "120px",
          dataIndex: "drugsSpec"
        },
        {
          title: "批号",
          align: "left",
          width: "80px",
          dataIndex: "drugLotNo"
        },
        {
          title: "类型",
          align: "center",
          width: "80px",
          dataIndex: "drugType",
          scopedSlots: { customRender: "drugType" }
        },
        {
          title: "销售单价",
          align: "center",
          width: "80px",
          dataIndex: "salePrice"
        },
        {
          title: "数量",
          align: "center",
          width: "80px",
          dataIndex: "saleNum"
        },
        {
          title: "销售金额",
          align: "center",
          width: "80px",
          needTotal: true,
          dataIndex: "saleAmount"
        },
        {
          title: "底价",
          align: "center",
          width: "100px",
          needTotal: true,
          hidden: !isZbj,
          dataIndex: "customFloorAmount"
        },
        {
          title: "税金",
          align: "center",
          width: "100px",
          needTotal: true,
          hidden: !isZbj,
          dataIndex: "customGapUpAmount"
        },
        {
          title: "资保金应收",
          align: "center",
          width: "90px",
          hidden: !isZbj,
          dataIndex: "customBond"
        },
        {
          title: "部门",
          align: "center",
          hidden: deptType == "ZS",
          width: "90px",
          children: [
            {
              title: "结算价",
              align: "center",
              width: "90px",
              dataIndex: "deptPrice"
            },
            {
              title: "结算标准",
              align: "center",
              width: "90px",
              dataIndex: "deptRate"
            },
            {
              title: "结算金额",
              align: "center",
              width: "90px",
              dataIndex: "deptSettleAmount"
            }
          ]
        },
        {
          title: "渠道商",
          align: "center",
          width: "120px",
          hidden: deptType == "ZY",
          children: [
            {
              title: "结算价",
              align: "center",
              width: "120px",
              hidden: saleManId,
              dataIndex: "settlePrice"
            },
            {
              title: "结算标准",
              align: "center",
              width: "120px",
              hidden: saleManId,
              dataIndex: "settleRate"
            },
            {
              title: "结算金额",
              align: "center",
              width: "130px",
              needTotal: true,
              hidden: saleManId,
              dataIndex: "settleAmount"
            }
          ]
        }
      ]
    },
    /**
     * 打开票折弹框
     * @param record
     */
    openDiscountModal(record) {
      this.formItem = record
      this.discountModal = true
    },
    /**
     * 票折提交
     */
    handleDiscountSubmit() {
      let params = {
        orderId: this.queryParam.orderId,
        orderDetailsId: this.formItem.orderDetailsId
      }
      this.$refs["discountForm"].submit(params, this.handleDiscountSuccess)
    },
    /**
     * 票折提交成功
     */
    handleDiscountSuccess() {
      this.$refs["orderDrugs"].refresh()
      this.discountModal = false
    },
    /**
     * 删除商品
     */
    delDiscount(record, index) {
      this.http.confirm(this, {
        content: "确认删除吗?",
        url: api.hyOrderDetails.deleteById,
        data: {
          orderDetailsId: record.orderDetailsId
        },
        success: (me, data) => {
          this.$refs["orderDrugs"].refresh()
        }
      })
    },
    renderTdBackground(record, color) {
      if (record.saleManId) {
        return {
          style: {
            "background-color": color
          }
        }
      }
    },
    openTuihuoHuikuanModal(record) {
      this.$set(record, "returnType", 1)
      this.formItem = record
      this.tuihuoHuikuanModal = true
    },
    openChaiModal(record) {
      this.$set(record, "returnType", 1)
      this.formItem = record
      this.chaiModal = true
    },
    //文件上传回调
    uploadSuccess(fileUrl, files, fileIds) {
      this.fileIds = fileIds
    },
    handleSubmit() {
      let params = {
        orderId: this.queryParam.orderId,
        orderDetailsId: this.formItem.orderDetailsId,
        yfkAmount: this.formItem.returnAmount
      }
      this.$refs["returnForm"].submit(params, this.handleSuccess)
    },
    handleChaiFenSubmit() {
      let params = {
        orderId: this.queryParam.orderId,
        orderDetailsId: this.formItem.orderDetailsId
      }
      this.$refs["chaiForm"].submit(params, this.handleChaiSuccess)
    },
    handleSuccess() {
      this.$refs["orderDrugs"].refresh()
      this.$refs["returnDetails"].refresh(false)
    },
    handleChaiSuccess() {
      this.$refs["orderDrugs"].refresh()
      this.chaiModal = false
    },
    processImg(order) {
      this.processInstanceId = order.processInstanceId
      this.processImageVisible = true
    },
    //重新提交
    completeTask(paymentId) {
      this.http.confirm(this, {
        content: "确认提交审批吗?",
        url: api.tasktodo.complete,
        requestBody: true,
        data: {
          taskId: paymentId.taskId
        },
        success: function(me, data) {
          me.$refs.table.refresh()
        }
      })
    },
    startProcess(record) {
      this.http.confirm(this, {
        content: "确认提交审批吗?",
        url: api.processInstance.start,
        requestBody: true,
        data: {
          handlerName: "hyAskAmountStartProcessHandler",
          businessKey: record.paymentId
        },
        success: (me, data) => {
          this.$refs.returnDetails.refresh()
        }
      })
    },
    /**
     * 撤销拆分
     */
    cancelChaiFen(order) {
      this.http.confirm(this, {
        content: "确认撤销拆分吗?",
        url: api.hyOrderDetails.cancelChaiFen,
        data: {
          orderDetailsId: order.orderDetailsId
        },
        success: function(me, data) {
          me.$refs.returnDetails.refresh()
          me.$refs["orderDrugs"].refresh(false)
        }
      })
    },
    /**
     * 撤销回款
     * @param order
     */
    revokeHandle(order) {
      this.http.confirm(this, {
        content: "确认撤销回款吗?",
        url: api.hyOrderDetails.get,
        data: {
          returnId: order.returnId
        },
        success: function(me, data) {
          me.$refs.returnDetails.refresh()
        }
      })
    },
    addTuibu(record, saleType) {
      this.tuiFormItem = record
      this.saleType = saleType
      if (saleType == 3) {
        this.$nextTick(() => {
          if (this.tuiFormItem.saleNum < 0) {
            this.tuiFormItem.saleNum = this.tuiFormItem.saleNum * -1
          }
        })
      }
      this.tuihuoModal = true
    },
    deleteTuibu(record) {
      this.http.confirm(this, {
        content: "确认删除吗?",
        url: api.hyOrderDetails.get,
        data: {
          orderDetailsId: record.orderDetailsId
        },
        success: function(me, data) {
          me.$refs["orderDrugs"].refresh()
        }
      })
    },
    tuiHandleSubmit() {
      let params = {
        orderDetailsId: this.tuiFormItem.orderDetailsId,
        saleNum: this.tuiFormItem.saleNum,
        saleType: this.saleType
      }
      this.$refs["tuibuForm"].submit(params, this.tuiHandleSuccess)
    },
    tuiHandleSuccess() {
      this.$refs["orderDrugs"].refresh()
      this.tuihuoModal = false
    },
    tuiHandleCancel() {
      this.tuihuoModal = false
    }
  }
}
</script>